package com.jmz.apt.api.dao;

import com.jmz.apt.api.Provider.AdminRoleProvider;
import com.jmz.apt.api.dto.impl.AdminRoleDto;
import com.jmz.apt.api.dto.impl.AdminUserDto;
import com.jmz.apt.api.entity.dbEntity.*;
import com.jmz.apt.api.message.request.RequestMessage;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * Created by fanglexue on 2019/10/30.
 */

@Mapper
public interface AdminRoleDao {

    @SelectProvider(type = AdminRoleProvider.class, method = "findUsers")
    public List<JmzAdminUser> findUsers(Integer roleId);

    @SelectProvider(type = AdminRoleProvider.class, method = "findResources")
    public List<JmzAdminResource> findResources(Integer roleId);

    @SelectProvider(type = AdminRoleProvider.class, method = "findUsers")
    public List<JmzAdminRole> findRoleList(RequestMessage requestMessage);

    @SelectProvider(type = AdminRoleProvider.class, method = "saveAdminRole")
    public List<JmzAdminRole> saveAdminRole(RequestMessage requestMessage);

    /**
     * Find role information with the given role id;
     *
     * @param roleId
     * @return
     */
    @Select("SELECT * FROM jmz_admin_role WHERE id = #{roleId}")
    @Results(
            {
                    @Result(id = true, column = "id", property = "id"),
                    @Result(column = "name", property = "name"),
                    @Result(column = "id", property = "resources", many = @Many(select = "com.jmz.apt.api.dao.AdminRoleDao.findResources", fetchType = FetchType.LAZY)),
                    @Result(column = "id", property = "users", many = @Many(select = "com.jmz.apt.api.dao.AdminRoleDao.findUsers", fetchType = FetchType.LAZY)),
            }
    )
    public JmzAdminRole findRoleOne(Integer roleId);


    @Select("select * from jmz_admin_role ")
    List<JmzAdminRole> getRoles();

    @DeleteProvider(type = AdminRoleProvider.class, method = "deleteUserRole")
    int deleteUserRole(@Param("ids") List<Integer> ids);

    @DeleteProvider(type = AdminRoleProvider.class, method = "deleteResourceRole")
    int deleteResourceRole(@Param("ids") List<Integer> ids);

    @DeleteProvider(type = AdminRoleProvider.class, method = "deleteRole")
    int deleteRole(@Param("ids") List<Integer> ids);


    @Insert("insert into jmz_admin_role(name,description,create_time,update_time) values(#{name},#{description},now(),now()) ")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void saveRole(AdminRoleDto roleDto);

    @Update("update jmz_admin_role  set name = #{name},update_time = NOW(),description =#{description} where id = #{id} ")
    void updateRole(AdminRoleDto roleDto);

    @InsertProvider(type = AdminRoleProvider.class, method = "insertUserRoleBatch")
    int insertUserRoleBatch(@Param("userIds") List<Integer> userIds, @Param("roleId") Integer roleId);

    @InsertProvider(type = AdminRoleProvider.class, method = "insertResourceRoleBatch")
    int insertResourceRoleBatch(@Param("resourceIds") List<Integer> resourceIds, @Param("roleId") Integer roleId);

    @Select("select  t.id as id,t.name as name from jmz_admin_user t where t.status = '1' ")
    List<AdminUserDto> selectUsers();

    @Select("select t.id as id,t.admin_Id as adminId,t.role_Id as roleId from jmz_admin_user_roles t where t.role_Id = #{roleId} and t.admin_Id = #{adminId}")
    JmzAdminUserRoles selectOneByUserIdAndRoleId(@Param("roleId") Integer roleId, @Param("adminId") Integer adminId);

    @Select({"<script>" +
            "SELECT id ,name,description FROM jmz_admin_role where 1 = 1" +
            "<if  test= \"id != null and id != ''\"> " +
            " and id = #{id} " +
            " </if> " +
            "<if  test= \"name != null and name != ''\"> " +
            " and name = #{name} " +
            " </if> " +
            "</script>"})
    JmzAdminRole findOneByRole(AdminRoleDto roleDto);

    @Select("select t.id as id,t.resource_id AS resourceId,t.role_Id as roleId from jmz_admin_role_resource t where t.role_Id = #{roleId} and t.resource_id = #{resourceId} ")
    JmzAdminRoleResource selectOneByResourceIdAndRoleId(@Param("roleId") Integer roleId, @Param("resourceId") Integer resourceId);

    @Select("SELECT id ,name,description FROM jmz_admin_role where id != #{id} and name = #{name}")
    JmzAdminRole findOneNotOwnId(AdminRoleDto roleDto);
}
